package cn.xaut.周赛.m228;

import java.util.Arrays;

/**
 * 5676. 生成交替二进制字符串的最少操作数
 */
public class demo5676 {

    public int work(char c, String s) {
        int res = 0;
        for (char x : s.toCharArray()) {
            if (c != x)
                res ++;
            c ^= 1;
        }
        return res;
    }

    // b站大雪菜的方法
    public int minOperations2(String s) {
        return Math.min(work('1', s), work('0', s));
    }
    
    public int minOperations(String s) {
        
        int len = s.length();
        if (len < 2)
            return 0;

        int count1 = 0, count2 = 0;
        char[] cs = s.toCharArray();
        char[] cs2 = s.toCharArray();
        for (int i = 0; i < len; i++) {
            if (i % 2 == 0 && cs[i] == '1') {
                cs[i] = '0';
                count1 ++;
            } else if (i % 2 != 0 && cs[i] == '0') {
                cs[i] = '1';
                count1 ++;
            }
        }
        for (int i = 0; i < len; i++) {
            if (i % 2 == 0 && cs2[i] == '0') {
                cs2[i] = '1';
                count2 ++;
            } else if (i % 2 != 0 && cs2[i] == '1') {
                cs2[i] = '0';
                count2 ++;
            }
        }
        System.out.println("------------");
        System.out.println(Arrays.toString(s.toCharArray()));
        System.out.println(Arrays.toString(cs));
        System.out.println(Arrays.toString(cs2));
        System.out.println(count1);
        System.out.println(count2);

        return Math.min(count1, count2);
    }

    public static void main(String[] args) {

        System.out.println(new demo5676().minOperations("0100"));
        System.out.println(new demo5676().minOperations("10"));
        System.out.println(new demo5676().minOperations("1111"));
        System.out.println(new demo5676().minOperations("110010"));
        System.out.println(new demo5676().minOperations("10010100"));
        System.out.println((int)'0');           // 48
        System.out.println((int)'0' ^ 1);       // 49
        System.out.println((int)'0' ^ 1 ^ 1);   // 48
    }
}
